package com.web.exiaodao.remote.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.web.exiaodao.remote.service.ApiUserService;
import com.web.webstart.base.util.*;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.exiaodao.remote.service.ApiWishService;
import com.web.exiaodao.remote.vo.WishVo;
import com.web.exiaodao.business.entity.Wish;

/**
 * @author eason.zt
 * @version V1.0
 * @Title: ApiWishController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 心愿单信息接口
 * @date 2015年3月23日 下午1:00:00
 */
@Api(value = "Wish", description = "心愿单接口", position = 10)
@Controller
@RequestMapping("/api/wish")
public class ApiWishController extends BaseController {

    @Autowired
    private ApiUserService userService;
    @Autowired
    private ApiWishService wishService;

    /**
     * @return
     * @Title: findWishList
     * @Description: 查询所有心愿单信息
     */
    @ApiOperation(value = "查询所有心愿单", notes = "查询所有心愿单信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findWishList", method = RequestMethod.POST)
    public XaResult<List<WishVo>> findWishList(
            @ApiParam("用户Id,字段名:userId") @RequestParam(value = "userId") Long userId,
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize
    ) throws BusinessException {
        XaResult<List<WishVo>> xr=new XaResult<>();
        if(XaUtil.isEmpty(userId)){
            xr.error("用户Id不能为空！");
            return xr;
        }
        if (XaUtil.isEmpty(userService.validUserId(userId+""))) {
            xr.error("用户Id不存在！");
            return xr;
        }
        try {
            Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'createTime',direction:'DESC'}]");
            Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{'search_EQ_userId':'" + userId + "'}", "search_");
            return wishService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                xr.error(e.getMessage());
            } else {
                xr.error(XaConstant.Message.error);
            }
        }
        return xr;
    }

    /**
     * @return
     * @Title: save
     * @Description: 新增一条实体
     * //@param userId
     * //@param model
     */
    @ApiOperation(value = "新增心愿单", notes = "供前端前期填充数据测试使用,上线后删除;新增心愿单,当返回的code=1时，保存成功后object返回对象")
    @ResponseBody
    @RequestMapping(value = "saveWish", method = RequestMethod.POST)
    public XaResult<WishVo> saveWish(
            @ApiParam("用户Id,字段名:userId") @RequestParam(value = "userId") Long userId,
            @ApiParam("wishDate,字段名:心愿时间") @RequestParam(value = "wishDate", required = false) String wishDate,
            @ApiParam("心愿内容,字段名:context") @RequestParam(value = "context") String context,
            HttpServletRequest request
    ) throws BusinessException {
        XaResult<WishVo> xr = new XaResult<>();
        if(XaUtil.isEmpty(userId)){
            xr.error("用户Id不能为空！");
            return xr;
        }
        if (XaUtil.isEmpty(userService.validUserId(userId+""))) {
            xr.error("用户Id不存在！");
            return xr;
        }
        if (XaUtil.isEmpty(context)) {
            xr.setMessage("内容不能为空");
            return xr;
        }
        if (XaUtil.isNotEmpty(wishDate)) {
            if (!Validator.isDateToSecond(wishDate)) {
                xr.setMessage("时间格式不正确");
                return xr;
            }
        } else {
            wishDate = DateUtils.getToday(DateUtils.YYYYMMDDHHMMSS);
        }
        Wish model = new Wish();
        model.setWishDate(wishDate);
        model.setContext(context);
        model.setUserId(userId);
        return wishService.createModel(model);
    }

    /**
     * @return
     * @Title:
     * @Description: 修改一条实体
     * //@param userId
     * //@param model
     */
    @ApiOperation(value = "修改心愿单", notes = "修改心愿单,当返回的code=1时，保存成功后object返回对象")
    @ResponseBody
    @RequestMapping(value = "updateWish", method = RequestMethod.POST)
    public XaResult<WishVo> updateWish(
            @ApiParam("wishDate,字段名:心愿时间") @RequestParam(value = "心愿时间", required = false) String wishDate,
            @ApiParam("心愿内容,字段名:context") @RequestParam(value = "context") String context,
            @ApiParam("唯一编号,字段名:tId") @RequestParam(value = "tId") Long tId,
            HttpServletRequest request
    ) throws BusinessException {
        XaResult<WishVo> xr = new XaResult<>();
        if (XaUtil.isEmpty(context)) {
            xr.setMessage("内容不能为空");
            return xr;
        }
        Wish model = new Wish();
        model.setWishDate(wishDate);
        model.setContext(context);
        model.setId(tId);
        return wishService.updateModel(model);
    }

    /**
     * @param status 操作类型:-1锁定,0无效,1正常,2发布,3删除,默认删除操作
     * @return
     * @Title: operateWishById
     * @Description: 操作一个实体状态, 根据status进行操作
     * //@param modelId
     */
    @ApiOperation(value = "根据ID修改心愿单状态", notes = "修改心愿单状态,当返回的code=1时，保存成功后object返回对象")
    @ResponseBody
    @RequestMapping(value = "operateWishById", method = RequestMethod.POST)
    public XaResult<WishVo> operateWishById(
            HttpServletRequest request,
            @ApiParam("编号,字段名:modelIds,必填") @RequestParam(value = "modelId") String modelIds,
            @ApiParam("操作类型,字段名:status,-1锁定,0无效,1正常,2发布,3删除,选填,默认删除操作") @RequestParam(defaultValue = "3") Integer status
    ) throws BusinessException {
        return wishService.multiOperate(modelIds, status);
    }
}

